package cn.jiayen.example.controller;

import cn.jiayen.mvc.page.PagerVO;
import cn.jiayen.example.service.UserService;
import cn.jiayen.example.views.dto.UserDTO;
import cn.jiayen.example.views.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by jiayen on 2018/5/5.
 */
@Api(tags = "User", description = "",value = "User")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @ApiOperation(value = "User-添加",notes = "添加")
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public UserVO addUser(@Valid @RequestBody UserDTO userDTO){
        UserVO result  =   userService.add(userDTO);
        return  result;
    }

    @ApiOperation(value = "User-删除(多个已逗号隔开)",notes = "删除(多个已逗号隔开)")
    @RequestMapping(value = "/user/{ids}",method = RequestMethod.DELETE)
    public void deleteUserById(@PathVariable String ids){
        userService.deleteById(ids);
    }

    @ApiOperation(value = "User-修改",notes = "修改")
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public UserVO updateUser(@Valid @RequestBody UserDTO user){
        UserVO result  =   userService.update(user);
        return  result;
    }

    @ApiOperation(value = "User-获取单个",notes = "获取单个")
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public UserVO getUserById(@PathVariable Integer id){
        UserVO user = userService.getById(id);
        return  user;
    }
    @ApiOperation(value = "User-获取列表",notes = "获取列表")
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public PagerVO<UserVO>  getUserList(@ApiParam(value = "页码从1开始")@RequestParam(required = false,defaultValue = "1") Integer page
            ,@ApiParam(value = "每页数量")@RequestParam(required = false,defaultValue = "10") Integer  pageSize){
        PagerVO<UserVO>  pagerVO = userService.getUserList(page,pageSize);
        return  pagerVO;
    }


}
